package com.totcy.atom.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.totcy.atom.R;
import com.totcy.atom.bean.MineMenuModel;
import com.totcy.atom.bean.User;
import com.totcy.atom.utils.glide.GlideUtil;
import com.totcy.baselibrary.adapter.BaseLoadMoreAdapter;
import com.totcy.baselibrary.adapter.BaseViewHolder;
import com.totcy.baselibrary.widget.CircleImageView;

import java.util.List;

/**
 * description: 我的界面菜单适配器
 *
 * @author: Totcy
 * date: 2017/12/1 15:16
 * upd 2017/12/1
 */

public class MineMenuAdapter extends BaseLoadMoreAdapter<MineMenuModel, BaseViewHolder, BaseViewHolder> {

    private User user;
    private BaseViewHolder headerViewHolder;

    public BaseViewHolder getHeaderViewHolder() {
        return headerViewHolder;
    }

    public MineMenuAdapter(Context mContext, List<MineMenuModel> mDatas, UserInfoClickListener mUserInfoClickListener,View headView) {
        super(mContext, mDatas);
        this.mUserInfoClickListener = mUserInfoClickListener;
        this.headerViewHolder = new BaseViewHolder(headView);
    }

    @Override
    public boolean hasHeader() {
        return true;
    }

    @Override
    protected BaseViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        return new BaseViewHolder(getItemView(R.layout.item_mine_menu, parent));
    }

    @Override
    protected BaseViewHolder onCreateHeaderViewHolder(ViewGroup parent, int viewType) {
        return headerViewHolder;
    }

    @Override
    protected void onBindItemViewHolder(BaseViewHolder holder, int position) {
        MineMenuModel model = mDatas.get(position);

        holder.setImageResource(R.id.iv_menuIcon, model.getMenuIcon());
        holder.setText(R.id.tv_menuName, model.getMenuName());

    }

    @Override
    protected void onBindHeaderViewHolder(BaseViewHolder holder) {
        if (user != null && !TextUtils.isEmpty(user.getUserImage())) {
            GlideUtil.loadImage(mContext, user.getUserImage(), (CircleImageView) holder.getView(R.id.iv_userImg), false);
        }
        holder.setText(R.id.tv_userName, user.getUserName());

        holder.setOnClickListener(R.id.iv_userImg, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mUserInfoClickListener != null) {
                    mUserInfoClickListener.onHeadImgSelect();
                }
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mUserInfoClickListener != null) {
                    mUserInfoClickListener.onUserInfoClick();
                }
            }
        });
    }

    /**
     * 更新用户信息
     *
     * @param user
     */
    public void updateUserInfo(User user) {
        this.user = user;
        notifyDataSetChanged();
    }
    public void setUserInfo(User user) {
        this.user = user;
    }

    private UserInfoClickListener mUserInfoClickListener;

    public interface UserInfoClickListener {
        void onUserInfoClick();

        void onHeadImgSelect();
    }
}
